/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "frm_control_TPageControl.h"
using namespace frm;
using namespace control;

// TPageControlClient /////////////////////////////////////////////////////////

//
TPageControlClient::TPageControlClient(TPageControl *PageControl) : TPanel(PageControl->GetWindow()) 
{ 
  this->PageControl = PageControl;

  //SetBorderMM(10,10,10,10);

  struct TStyle : public TPanel::TStyle
  {
    TStyle()
    {
      Filler.Set(filler::TFrameRect(0,0,0));
    }
  };
  static TStyle Style; SetStyle(Style);
}

//
TPageControlClient::~TPageControlClient() 
{ 
  //DeleteChildren();
}

//
void TPageControlClient::AddChildFirst(TComponent *Add,bool EnDelete)
{ 
  TPage *Page = dynamic_cast<TPage*>(Add); if (Page) {
    Page->PageControl = PageControl;
    PageControl->Buttons->AddChildFirst(Page->Button,false);
  }
  TPanel::AddChildFirst(Add,EnDelete);
}

//
void TPageControlClient::AddChildLast(TComponent *Add,bool EnDelete)
{ 
  TPage *Page = dynamic_cast<TPage*>(Add); if (Page) {
    Page->PageControl = PageControl;
    PageControl->Buttons->AddChildLast(Page->Button,false);
  }
  TPanel::AddChildLast(Add,EnDelete);
}

//
void TPageControlClient::AddChildBefore(TComponent *Add,TComponent *Curr,bool EnDelete) 
{ 
  TPage *AddPage = dynamic_cast<TPage*>(Add); if (AddPage) {
    AddPage->PageControl = PageControl;
    
    TPage *CurrPage = dynamic_cast<TPage*>(Curr); if (AddPage) {
      PageControl->Buttons->AddChildBefore(AddPage->Button,CurrPage->Button,false);
    }
  }
  TPanel::AddChildBefore(Add,Curr,EnDelete);
}

//
void TPageControlClient::AddChildAfter(TComponent *Add,TComponent *Curr,bool EnDelete) 
{ 
  TPage *AddPage = dynamic_cast<TPage*>(Add); if (AddPage) {
    AddPage->PageControl = PageControl;
    
    TPage *CurrPage = dynamic_cast<TPage*>(Curr); if (AddPage) {
      PageControl->Buttons->AddChildAfter(AddPage->Button,CurrPage->Button,false);
    }
  }
  TPanel::AddChildAfter(Add,Curr,EnDelete);
}

//
void TPageControlClient::DeleteChild(int N) 
{ 
  TPage *DelPage = dynamic_cast<TPage*>(&GetChild(N)); if (DelPage) {
    DelPage->PageControl = NULL;
    PageControl->Buttons->DeleteChild(DelPage->Button);
  }
  TPanel::DeleteChild(N);
}

// TPageControlButtons ////////////////////////////////////////////////////////

//
TPageControlButtons::TPageControlButtons(TPageControl *PageControl) : TPanel(PageControl->GetWindow()) 
{ 
  this->PageControl = PageControl;
}

// TPageString ////////////////////////////////////////////////////////////////

/*/
TPageString::TPageString(TPageButton *Parent) : TString(Parent->GetWindow())
{
  Parent->AddChildLast(this,true);
  SetValue(" page ");
}
*/

// TPageButton ////////////////////////////////////////////////////////////////

//
TPageButton::TPageButton(TPage *Page) : TButton(Page->GetWindow())
{ 
  this->Page = Page;
  //this->Width_Fixed(250).Height_Fixed(100)//Fixed(20)
  this->Width_ChildrenSum().Height_ChildrenMax()
   .DirectChildren_Horz()
   .WAlignChildren_Center().HAlignChildren_Center();
  SetKeepDown(true);
  
  //String = new TPageString(this);
  
  struct TStyle 
  {
    TButton::TStyle Button;
    //TString::TStyle String;
    //
    TStyle()
    {
      Button.Normal.Filler.SetNull();
      Button.Down.Filler.Set(filler::TColor(200,200,200));
      Button.Flat.Filler.SetNull();
      Button.Disable.Filler.SetNull();
      
      //String.Normal.Cursor.SetNull();//(cursor::TCursorWin(IDC_ARROW));
      //String.SetFontDefault();
    }
  };
  static TStyle Style;
  
  this  ->SetStyle(Style.Button);
  //String->SetStyle(Style.String);
}

//
void TPageButton::UpdateDown(bool V) 
{ 
  Page->UpdateVisible(V); 
}

// TPage //////////////////////////////////////////////////////////////////////

//
TPage::TPage(TWindow &Window) : TPanel(Window)
{
  PageControl = NULL;
  SetVisible(false);
  Width_Fixed(200).Height_Fixed(200);

  Button = new TPageButton(this);
}

//
TPage::~TPage()
{
  //DeleteChildren();
  delete Button;
}

//
void TPage::UpdateVisible(bool V)
{
  if (PageControl->visiblePage == this) { 
    return;
  }

  if (V) {
    bool EnShow = true;
    PageControl->Events.Get().TEvents_BeforeShowPage(
      TPageControl::TEvents::TShowPage(PageControl->Events,this,EnShow)
    );
    if (!EnShow) {
      return;
    }
  }

  TBeginPaint BP(this);
  //TPanel::UpdateVisible(V); V = GetVisible();

  if (V) {
    PageControl->visiblePage = NULL; 
    
    for (int N = 0; N < PageControl->Buttons->GetChildrenCount(); N++) {
      TButton *ChButton = dynamic_cast<TButton*>(&PageControl->Buttons->GetChild(N)); 
      if (ChButton && ChButton->GetDown()) { ChButton->UpdateDown(false);}
    }
    PageControl->visiblePage = this;
  }
  
  Button->control::TButton::UpdateDown(V);
  //CalcAllSize();
  TPanel::UpdateVisible(V);

  if (V) {
    bool EnShow = true;
    PageControl->Events.Get().TEvents_AfterShowPage(
      TPageControl::TEvents::TShowPage(PageControl->Events,this,EnShow)
    );
  }
}

// TPageControl ///////////////////////////////////////////////////////////////

//
TPageControl::TPageControl(TWindow &Window) : TPanel(Window),Events(*this)
{
  visiblePage = NULL;
  
  this->Width_Fixed(400).Height_Fixed(400)
   .WAlignChildren_Begin().HAlignChildren_Begin();
  
  //Client  = new TClient(this); AddChildLast(Client); 
  //Buttons = new TPanel(GetWindow()); AddChildLast(Buttons); 

/*************
  this->DirectChildren_Horz();

  Buttons = new TPageControlButtons(this); AddChildLast(Buttons); Buttons->
    Height_ChildrenSum().Width_ChildrenMax()
   .DirectChildren_Vert()
   .WAlignChildren_Begin().HAlignChildren_Begin();

  Client = new TPageControlClient(this); AddChildLast(Client); Client->
    Height_Parent().Width_SiblingsRem();
***************/

  
/*********
  this->DirectChildren_Vert();

  Buttons = new TPageControlButtons(this); AddChildLast(Buttons); Buttons->
    Width_ChildrenSum().Height_ChildrenMax()
   .DirectChildren_Horz()
   .WAlignChildren_Begin().HAlignChildren_Center();

  Client = new TPageControlClient(this); AddChildLast(Client); Client->
    Width_Parent().Height_SiblingsRem();
***********/



  this->DirectChildren_Horz();
 
  Client = new TPageControlClient(this); AddChildLast(Client); Client->
    Height_Parent().Width_SiblingsRem();

  Buttons = new TPageControlButtons(this); AddChildLast(Buttons); Buttons->
    Height_ChildrenSum().Width_ChildrenMax()
   .DirectChildren_Vert()
   .WAlignChildren_Begin().HAlignChildren_Begin();

 
/*************
  this->DirectChildren_Vert();

  Client = new TPageControlClient(this); AddChildLast(Client); Client->
    Width_Parent().Height_SiblingsRem();

  Buttons = new TPageControlButtons(this); AddChildLast(Buttons); Buttons->
    Width_ChildrenSum().Height_ChildrenMax()
   .DirectChildren_Horz()
   .WAlignChildren_Begin().HAlignChildren_Center();
************/   
}

//
TPageControl::~TPageControl()
{
  Buttons->DeleteChildren();
}

//
void TPageControl::SetStyle(TStyle &V)
{
  TPanel::SetStyle(V);

  Client ->SetStyle(V.Client);
  Buttons->SetStyle(V.Buttons);
}

//
void TPageControl::SetEButtons(EButtons V)
{
  switch (V)
  {
    case EButtons_Left:
      this->DirectChildren_Vert();
     
      Buttons->
        Width_ChildrenSum().Height_ChildrenMax()
       .DirectChildren_Horz()
       .WAlignChildren_Begin().HAlignChildren_Center();

      Client->
        Width_Parent().Height_SiblingsRem();
    break;
  
    case EButtons_Top:
      this->DirectChildren_Vert();
     
      Buttons->
        Width_ChildrenSum().Height_ChildrenMax()
       .DirectChildren_Horz()
       .WAlignChildren_Begin().HAlignChildren_Center();

      Client->
        Width_Parent().Height_SiblingsRem();
    break;

    case EButtons_Right:
    break;

    case EButtons_Bottom:
    break;
  }
}

// TPageControl /////////////////////////////////////////////////////////////////

//
void TPageControl::TStyle::SetFromParent(frm::TStyle *Parent_)
{
  if (!Parent_) {
    return;
  }

  TStyle *Parent = dynamic_cast<TStyle*>(Parent_);
  
  TPanel::TStyle::SetFromParent(Parent);
  
  Client.SetFromParent (&Parent->Client);
  Buttons.SetFromParent(&Parent->Buttons);
}

//
svl::TInfoParam TPageControl::TStyle::AddInfoTo(svl::TInfoParam Info)
{
  return TPanel::TStyle::AddInfoTo(Info)
   .Add("Client",  Client)
   .Add("Buttons", Buttons)
  ;
}


// TPage /////////////////////////////////////////////////////////////////

//
void TPage::TStyle::SetFromParent(frm::TStyle *Parent_)
{
  if (!Parent_) {
    return;
  }

  TStyle *Parent = dynamic_cast<TStyle*>(Parent_);
  
  TPanel::TStyle::SetFromParent(Parent);
  
  //Button.SetFromParent(&Parent->Button);
}

//
svl::TInfoParam TPage::TStyle::AddInfoTo(svl::TInfoParam Info)
{
  return TPanel::TStyle::AddInfoTo(Info);
   //.Add("Button", Button)
  //;
}